package org.sky.generator;

import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.logging.log4j.Logger;
import org.sky.generator.config.*;
import org.sky.util.JsonUtils;
import org.sky.util.LogUtils;
import org.yaml.snakeyaml.Yaml;

import java.util.Map;

/**
 * @author weifx
 * @Title: ConfigBean
 * @ProjectName springboot-platform
 * @Description: TODO{代码生成参数}
 * @date 2019/2/26 20:55
 * @Version 1.0
 */
@Data
@Accessors(chain=true)
public class GeneratorConfig {
    private static Logger logger = LogUtils.getLogger(GeneratorConfig.class);
    private final static String ORACLE="ORACLE";
    private final static String MYSQL="MYSQL";
    /**
     * 数据库类型
     */
    private String dbType;
    /**
     * 要生成的表
     */
    private String[] tables;
    /**
     * 前后台请求路径
     */
    private String requestUrl;
    /**
     * 作者
     */
    private String author;
    /**
     * Model代码生成器配置
     */
    private EntityGeneratorConfig entityGeneratorConfig;
    /**
     * MybatisXML代码生成器配置
     */
    private MybatisGeneratorConfig mybatisGeneratorConfig;
    /**
     * Mapper接口代码生成器配置
     */
    private MapperGeneratorConfig mapperGeneratorConfig;
    /**
     * Service代码生成器配置
     */
    private ServiceGeneratorConfig serviceGeneratorConfig;
    /**
     * Controller代码生成器配置
     */
    private ControllerGeneratorConfig controllerGeneratorConfig;
    /**
     * 视图层代码生成器配置
     */
    private ViewGeneratorConfig viewGeneratorConfig;

    private GeneratorConfig(){

    }

    private static GeneratorConfig generatorConfig = null;
    public static GeneratorConfig getInstance(){
        if(null == generatorConfig) {
            try {
                //加载配置文件
                Yaml applicationYml = new Yaml();
                Map appYml = applicationYml.load(GeneratorApplication.class.getClassLoader().getResourceAsStream("application.yml"));
                generatorConfig = JsonUtils.map2pojo((Map) appYml.get("generatorconfig"), GeneratorConfig.class);
                System.out.println(JsonUtils.obj2json(generatorConfig));
            } catch (Exception e) {
                logger.error("获取配置失败", e);
            } finally {
                return generatorConfig;
            }
        }else{
            return generatorConfig;
        }
    }
}
